package cn.chaohi.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.chaohi.common.ApiException;
import cn.chaohi.common.CodeEnum;
import cn.chaohi.common.Constants;
import cn.chaohi.controller.model.versionConfig.ConfigAndVersionReq;
import cn.chaohi.controller.model.versionConfig.ConfigAndVersionResp;
import cn.chaohi.dao.config.AllUpgradeConfigMapper;
import cn.chaohi.dao.config.PartUpgradeConfigMapper;
import cn.chaohi.dao.version.AllVersionInfoMapper;
import cn.chaohi.dao.version.PlusVersionInfoMapper;
import cn.chaohi.domain.config.AllUpgradeConfig;
import cn.chaohi.domain.config.PartUpgradeConfig;
import cn.chaohi.domain.version.AllVersionInfo;
import cn.chaohi.domain.version.PlusVersionInfo;
import cn.chaohi.model.Result;
import cn.chaohi.service.ConfigAndVersionService;

@Service
public class ConfigAndVersionServiceImpl implements ConfigAndVersionService {
	
	@Resource
	private AllUpgradeConfigMapper allUpgradeConfigMapper;
	@Resource
	private PartUpgradeConfigMapper partUpgradeConfigMapper;
	
	@Resource
	private AllVersionInfoMapper allVersionInfoMapper;
	
	@Resource
	private PlusVersionInfoMapper plusVersionInfoMapper;
	
	/**
	 * 获取配置信息和版本信息
	 */
	public Result checkConfigAndVersion (final ConfigAndVersionReq configAndVersionReq) {
		String marketId = configAndVersionReq.getMarketId();
		String wifiMac = configAndVersionReq.getWifiMac();
		String marketStoreId = configAndVersionReq.getMarketStoreId();
		String oldVersion = configAndVersionReq.getOldVersion();
		String version = "";
		String plusUpgrade = "";
		//总配置
		AllUpgradeConfig allUpgradeConfig = allUpgradeConfigMapper.findAllUpgradeConfig(marketId, marketStoreId);
		//部分升级时的配置
		PartUpgradeConfig partUpgradeConfig = partUpgradeConfigMapper.findPartUpgradeConfig(marketId, marketStoreId, wifiMac);
		
		if (allUpgradeConfig.getAllUpgrade().equals(Constants.AllOrPartUpgrade.All)) {
			version = allUpgradeConfig.getVersion();
			plusUpgrade = allUpgradeConfig.getPlusUpgrade();
		} else {
			if (null == partUpgradeConfig) {
				throw new ApiException(CodeEnum.PART_CONFIG_ISNULL);
			}
			version = partUpgradeConfig.getVersion();
			plusUpgrade = partUpgradeConfig.getPlusUpgrade();
		}
		
		//全量升级
		AllVersionInfo allVersionInfo = allVersionInfoMapper.findAllNotPlusVersionInfo(marketId, marketStoreId, version);
		//增量升级
		PlusVersionInfo plusVersionInfo = plusVersionInfoMapper.findPlusVersionInfo(marketId, marketStoreId, oldVersion, version);
		
		if (!allUpgradeConfig.getUpgradeDegrade().equals(Constants.UpgradeDegradeClose.CLOSE)) {
			if (plusUpgrade.equals(Constants.AllOrPlusUpgrade.All)) {
				if (null == allVersionInfo) {
					return new Result(CodeEnum.ALL_VERSION);				
				}
			} else {
				if (null == plusVersionInfo) {
					return new Result(CodeEnum.PLUS_VERSION);				
				}
			}
		}
		return null;
	}
	
	/**
	 * 获取配置信息和版本信息
	 */
	public ConfigAndVersionResp getConfigAndVersion (final ConfigAndVersionReq configAndVersionReq) {
		ConfigAndVersionResp configAndVersionResp = null;
		String marketId = configAndVersionReq.getMarketId();
		String wifiMac = configAndVersionReq.getWifiMac();
		String marketStoreId = configAndVersionReq.getMarketStoreId();
		String oldVersion = configAndVersionReq.getOldVersion();
		String version = "";
		String plusUpgrade = "";
		//总配置
		AllUpgradeConfig allUpgradeConfig = allUpgradeConfigMapper.findAllUpgradeConfig(marketId, marketStoreId);
		
		//升级关闭中
		if (allUpgradeConfig.getUpgradeDegrade().equals(Constants.UpgradeDegradeClose.CLOSE)) {
			return new ConfigAndVersionResp(allUpgradeConfig);
		}
		
		//部分升级时的配置
		PartUpgradeConfig partUpgradeConfig = partUpgradeConfigMapper.findPartUpgradeConfig(marketId, marketStoreId, wifiMac);
		
		if (allUpgradeConfig.getAllUpgrade().equals(Constants.AllOrPartUpgrade.All)) {
			version = allUpgradeConfig.getVersion();
			plusUpgrade = allUpgradeConfig.getPlusUpgrade();
		} else {
			version = partUpgradeConfig.getVersion();
			plusUpgrade = partUpgradeConfig.getPlusUpgrade();
		}
		
		//全量升级
		AllVersionInfo allVersionInfo = allVersionInfoMapper.findAllNotPlusVersionInfo(marketId, marketStoreId, version);
		//增量升级
		PlusVersionInfo plusVersionInfo = plusVersionInfoMapper.findPlusVersionInfo(marketId, marketStoreId, oldVersion, version);
		
		if (allUpgradeConfig.getAllUpgrade().equals(Constants.AllOrPartUpgrade.All)) {
			if (plusUpgrade.equals(Constants.AllOrPlusUpgrade.All)) { // 全部 -全量 
				configAndVersionResp = new ConfigAndVersionResp(allUpgradeConfig, allVersionInfo);
			} else {
				configAndVersionResp = new ConfigAndVersionResp(allUpgradeConfig, plusVersionInfo); // 全部 -增量 
			}
		} else {
			if (plusUpgrade.equals(Constants.AllOrPlusUpgrade.All)) { //部分 -全量 
				configAndVersionResp = new ConfigAndVersionResp(partUpgradeConfig, allVersionInfo);
			} else {
				configAndVersionResp = new ConfigAndVersionResp(partUpgradeConfig, plusVersionInfo); // 部分 -增量 
			}
		}
		return configAndVersionResp;
	}
}
